from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from . import serializers

# Create your views here.

class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    # 关闭分页处理
    pagination_class = None

    def get_queryset(self):
        """
            提供数据集
        """
        if self.action == 'list':
            # 提供给list方式,返回一级省份信息
            return Area.objects.filter(parent=None)
        else:
            # 提供给retreieve方法,提供给get_object使用
            return Area.objects.all()

    def get_serializer_class(self):
        """
            提供序列化器
        """
        if self.action == 'list':
            return serializers.AreaSerializer
        else:
            return serializers.SubAreaSerializer
